<template>
	<view>
		<ikz-loading :css-data="loading_css"></ikz-loading>
		<!--地址列表  -->
		<block v-if="address.length == 0">
			<view class="ikz-list-null">
				<view style="color:#888;font-size:28rpx">暂无收货地址，快去添加吧~</view>
			</view>
		</block>
		<block v-else v-for="(item, index) in address" :key="index">
			<view class="ikz-address">
				<view class="ikz-address-details" @click="radioChange" :data-id="item.id" :data-item="item">
					<view class="ikz-address-details-info">
						<view class="ikz-address-details-info-nick">
							<view class="ikz-address-details-info-nick-name">{{ item.name }}</view>
							<view>{{ item.mobile }}</view>
						</view>
						<view class="ikz-address-details-info-city">{{ item.group_name }}{{ item.detail }}</view>
					</view>
				</view>
				<view class="ikz-address-operation">
					<block v-if="item.defaults">
						<view class="ikz-address-operation-default">
							<image class="ikz-address-icon" src="/static/address/dui.png"></image>
							<view>默认地址</view>
						</view>
					</block>
					<block v-else>
						<view class="ikz-address-operation-nodefault" @click="radioDefault" :data-id="item.id">
							<image class="ikz-address-icon" src="/static/address/zero.png"></image>
							<view>设为默认地址</view>
						</view>
					</block>
					<view class="ikz-address-operation-option">
						<view class="ikz-address-operation-option-edit" :data-id="item.id" :data-name="item.name"
							:data-mobile="item.mobile" :data-detail="item.detail" :data-group_name="item.group_name"
							:data-defaults="item.defaults" @click="update" :data-index="index">
							<image class="ikz-address-icon" src="/static/address/save.png"></image>
							<view style="color: #000;">编辑</view>
						</view>
						<view class="ikz-address-operation-option-del" :data-id="item.id" :data-defaults="item.defaults"
							@click="deletes" :data-index="index">
							<image class="ikz-address-icon" src="/static/address/del.png"></image>
							<view style="color: #FA4D4D;">删除</view>
						</view>
					</view>
				</view>
			</view>
		</block>
		<!--地址列表  -->
		<!--新增地址  -->
		<view class="ikz-address-br"></view>
		<view class="ikz-address-add">
			<view class="ikz-address-add-add" @click="addAddress">
				<view>添加地址</view>
			</view>
		</view>
		<!--新增地址  -->
	</view>
</template>

<script>
	import ikz from '@/pages/Common/tools';
	import ikzLoading from '@/pages/Component/Public/Loading/loading.vue';
	var app = getApp();
	export default {
		data() {
			return {
				// 类型
				type:"",
				address: {},
				color: '',
				loading_css: {
					display: 'flex'
				},
			};
		},
		onLoad(e) {
			console.log(e.type);
			this.type = e.type
		},
		onShow: function() {
			this.loading_css.display = 'none';
			// 获取地址
			this.address = app.globalData.user_address.address_all;
			this.$forceUpdate();
			// uni.setNavigationBarColor({
			//   frontColor: '#ffffff',
			//   backgroundColor: this.color,
			//   animation: {
			//     duration: 400,
			//     timingFunc: 'easeIn''
			//   }
			// }); 
		},
		methods: {
			//选择地址事件
			radioChange: function(e) {
				var that = this;
				if(this.type == 1){
					var id = e.currentTarget.dataset.id;
					var current = e.currentTarget.dataset.item;
					const adress =  app.globalData.user_address.address_default.detail +  app.globalData.user_address.address_default.group_name
					if(adress == current.detail+current.group_name){
						uni.showToast({
							title:"当前地址为送货地址，请选择其他地址或者重新添加",
							icon:"none"
						})
					}else{
						
						app.globalData.address_Takedelivery = current
						uni.navigateBack({
							delta: 1
						});
					}
					// if(app.globalData.user_address.address_default.)
					// address_Takedelivery
					// var id = e.currentTarget.dataset.id;
					// var current = e.currentTarget.dataset.item;
					// //点击确定
					// app.globalData.user_address.address_default = current;
					// uni.navigateBack({
					// 	delta: 1
					// });
					return;
				}
				
				var id = e.currentTarget.dataset.id;
				var current = e.currentTarget.dataset.item;
				//点击确定
				app.globalData.user_address.address_default = current;
				uni.navigateBack({
					delta: 1
				});
			},
			//选择默认地址事件
			radioDefault: function(e) {
				uni.showLoading({
					title: '加载中'
				});
				var that = this;
				var id = e.currentTarget.dataset.id;
				//赋值当前地址
				var path = '/xyb/user_address/set_defaults';
				var data = {
					id: id
				};
				ikz.get(path, data, res => {
					if (res.data.code) {
						var address = that.address;
						for (var i = 0; i < address.length; i++) {
							if (address[i].id == id) {
								address[i].defaults = true;
							} else {
								address[i].defaults = false;
							}
						}
						app.globalData.user_address.address_all = address;
						that.address = address;
						this.$forceUpdate();
					} else {
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false
						});
					}
					uni.hideLoading();
				});
			},

			//修改收货地址
			update: function(e) {
				var url = '/pages/Component/Module/Address/Program/saveAddress/index';
				var id = e.currentTarget.dataset.id;
				var name = e.currentTarget.dataset.name;
				var mobile = e.currentTarget.dataset.mobile;
				var detail = e.currentTarget.dataset.detail;
				var group_name = e.currentTarget.dataset.group_name;
				var defaults = e.currentTarget.dataset.defaults;
				var index = e.currentTarget.dataset.index;
				var data = {
					id: id,
					name: name,
					mobile: mobile,
					detail: detail,
					group_name: group_name,
					defaults: defaults,
					index: index
				};
				ikz.nav(url, 'ln', data);
			},

			//删除收货地址
			deletes: function(e) {
				var that = this;
				var id = e.currentTarget.dataset.id;
				var defaults = e.currentTarget.dataset.defaults;
				var index = e.currentTarget.dataset.index;
				uni.showModal({
					title: '提示',
					content: '确定删除该地址吗？',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '加载中'
							})
							var path = '/xyb/user_address/del';
							var data = {
								id: id
							};
							ikz.get(path, data, res => {
								if (res.data.code) {
									var address = that.address;
									address.splice(index, 1);
									if (defaults) {
										if (address.length) {
											app.globalData.user_address.address_default = address[
												0]; //默认地址
											app.globalData.user_address.address_current = address[
												0]; //当前
										} else {
											app.globalData.user_address.address_default =
											''; //默认地址
											app.globalData.user_address.address_current = ''; //当前
										}
									} else {
										if (address.length) {} else {
											app.globalData.user_address.address_default =
											''; //默认地址
											app.globalData.user_address.address_current = ''; //当前
										}
									}
									app.globalData.user_address.address_all = address;
									that.address = address;
									that.$forceUpdate();
									uni.showToast({
										title: '地址删除成功',
										icon: 'success',
										duration: 2000
									});
								} else {
									uni.showModal({
										title: '提示',
										content: res.data.msg,
										showCancel: false
									});
								}
								uni.hideLoading();
							});
						}
					}
				});
			},
			//新增地址
			addAddress: function(e) {
				var url = '/pages/Component/Module/Address/Program/addAddress/index?type='+this.type;
				ikz.nav(url, 'ln');
			}
		}
	};
</script>

<style>
	/* pages/Personal/myaddress/myaddress.wxss */

	page {
		background-color: #eee;
	}

	/*地址列表  */
	.ikz-list-null {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 50rpx;
	}

	.ikz-address {
		width: 95%;
		margin: 10rpx auto;
		font-size: 28rpx;
		color: #2c302f;
		line-height: 50rpx;
		background: #fff;
		margin-bottom: 15rpx;
		border-radius: 15rpx;
	}

	.ikz-address-details-info-nick {
		display: flex;
		align-items: center;
	}

	.ikz-address-details-info-nick-name {
		line-height: 40rpx;
		margin: 0 20rpx 10rpx 0;
	}

	.ikz-address-details {
		line-height: 40rpx;
		display: flex;
		flex-wrap: nowrap;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
	}

	.ikz-address-choice-border {
		margin-left: 20rpx;
		width: 20%;
		display: flex;
		justify-content: flex-end;
	}

	.ikz-address-choice-border image {
		width: 50rpx;
		height: 50rpx;
	}

	.ikz-address-choice {
		line-height: 50rpx;
		margin-right: 10rpx;
		color: #4e4e4e;
		border: 1rpx solid #4e4e4e;
		width: 80rpx;
		height: 50rpx;
		text-align: center;
		border-radius: 5rpx;
	}

	.ikz-address-operation {
		border-top: 1rpx solid #eaeaea;
	}

	.ikz-address-operation {
		padding: 15rpx 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.ikz-address-operation-default {
		display: flex;
		align-items: center;
		color: #8a8a8a;
	}

	.ikz-address-operation-nodefault {
		display: flex;
		align-items: center;
		color: #8a8a8a;
	}

	.ikz-address-icon {
		width: 30rpx;
		height: 30rpx;
		margin-right: 20rpx;
	}

	.ikz-address-operation-option {
		display: flex;
		align-items: center;
	}

	.ikz-address-operation-option-edit {
		display: flex;
		align-items: center;
		color: #8a8a8a;
		margin-right: 40rpx;
	}

	.ikz-address-operation-option-del {
		display: flex;
		align-items: center;
		color: #8a8a8a;
	}

	/*地址列表  */
	.ikz-address-br {
		width: 100%;
		height: 100rpx;
	}

	/*新增按钮  */
	.ikz-address-add {
		width: 100%;
		font-size: 30rpx;
		display: flex;
		color: #fff;
		line-height: 80rpx;
		justify-content: space-around;
	}

	.ikz-address-add-add {
		display: flex;
		align-items: center;
		width: 90%;
		text-align: center;
		justify-content: center;
		height: 80rpx;
		border-radius: 50px;
		background: linear-gradient(to right, #FBAB7E, #F8C66D);
	}

	.ikz-address-details-info-city {
		color: #808080;
	}

	/*新增按钮  */
</style>
